package generalClasses;

import java.io.*;

/**
 * Program constants and data.
 * The class read it data from a local definition file.
 * @author
 *
 */
public class ProgramData {
	
//Constants Definitions
	//Input file properties
	private static String FILE_ADRESS="c:\\couponProperties.txt";
	
	//coupons states
	public static final int NOT_AVAILABLE=0;
	public static final int STILL_AVAILABLE=1;
	
	//coupons categories int
	public static final int FOOD=0;
	public static final int EVENTS=1;
	public static final int ELECTRICITY=2;
	public static final int VACATIONS=3;
	public static final int FASHION=4;
	public static final int ADULTS_ONLY=5;
	
	//coupons categories String
	private static final String _FOODstr = "Food";
	private static final String _EVENTSstr = "Events";
	private static final String _ELECTRICITYstr = "Electricity";
	private static final String _VACATIONSstr = "Vacations";
	private static final String _FASHIONstr = "Fashion";
	private static final String _ADULTS_ONLYstr = "Adults Only";
	private static final String _NO_CATEGORYstr = "NO_CATEGORY";
	
	//regions
	public static final int NORTH=0;
	public static final int CENTER=1;
	public static final int SOUTH=2;
	public static final int GENERAL=3;
	public static final int BACKUP=4;
	
	//server states
	public static final int OFFLINE=0;
	public static final int ONLINE=1;
	public static final int IN_SESSION=2;
	
	//server constants
	public static final int MAIN_SERVER=0;
	public static final int LOCAL_SERVER=1;
	public static final int BACKUP_SERVER=2;
	
	//Time constants
	public static final long ONE_MINUTE=60000;
	public static final long ONE_HOUR=3600000;
	public static final long ONE_DAY=86400000;
	
	//other variables
	public int COUPONS_FOR_MAIN_SERVER;

//Global servers variables
	private String mySqlUserName;	
	private String mySqlPassword;	
	
	private String mainServerAdress;
	private int mainServerPort;
	private String mainServerDbAdress;
	private String mainServerDbName;
	
	private String backupServerAdress;
	private int backupServerPort;
	private String backupServerDbAdress;
	private String backupServerDbName;
		
	private String northServerAdress;
	private int northServerPort;
	private String northServerDbAdress;
	private String northServerDbName;
	
	private String centerServerAdress;
	private int centerServerPort;
	private String centerServerDbAdress;
	private String centerServerDbName;
	
	private String southServerAdress;
	private int southServerPort;
	private String southServerDbAdress;
	private String southServerDbName;
	
	public ProgramData() {
		try 
		{
			File aFile=new File(FILE_ADRESS);
	      
			BufferedReader input =  new BufferedReader(new FileReader(aFile));
			try
			{	
				input.readLine();
				this.mySqlUserName=input.readLine();
				input.readLine();
				this.mySqlPassword=input.readLine();
				
				input.readLine();
				this.mainServerAdress=input.readLine();
				input.readLine();
				this.mainServerPort =Integer.parseInt(input.readLine());
				input.readLine();
				this.mainServerDbAdress="jdbc:mysql://"+input.readLine()+":3306/";
				input.readLine();
				this.mainServerDbName=input.readLine();
				
				input.readLine();
				this.backupServerAdress=input.readLine();
				input.readLine();
				this.backupServerPort=Integer.parseInt(input.readLine());
				input.readLine();
				this.backupServerDbAdress="jdbc:mysql://"+input.readLine()+":3306/";
				input.readLine();
				this.backupServerDbName=input.readLine();
				
				input.readLine();
				this.northServerAdress=input.readLine();
				input.readLine();
				this.northServerPort=Integer.parseInt(input.readLine());
				input.readLine();
				this.northServerDbAdress="jdbc:mysql://"+input.readLine()+":3306/";
				input.readLine();
				this.northServerDbName=input.readLine();
				
				input.readLine();
				this.centerServerAdress=input.readLine();
				input.readLine();
				this.centerServerPort=Integer.parseInt(input.readLine());
				input.readLine();
				this.centerServerDbAdress="jdbc:mysql://"+input.readLine()+":3306/";
				input.readLine();
				this.centerServerDbName=input.readLine();
				
				input.readLine();
				this.southServerAdress=input.readLine();
				input.readLine();
				this.southServerPort=Integer.parseInt(input.readLine());
				input.readLine();
				this.southServerDbAdress="jdbc:mysql://"+input.readLine()+":3306/";
				input.readLine();
				this.southServerDbName=input.readLine();
				input.readLine();
				this.COUPONS_FOR_MAIN_SERVER=Integer.parseInt(input.readLine());
			}
			finally
			{
				input.close();
			}
		}
		catch (IOException ex)
		{
			System.out.println("There was an error reading from file.");
	    }
	}

	public String getMySqlUserName() {
		return mySqlUserName;
	}

	public String getMySqlPassword() {
		return mySqlPassword;
	}

	public String getMainServerAdress() {
		return mainServerAdress;
	}

	public int getMainServerPort() {
		return mainServerPort;
	}

	public String getMainServerDbAdress() {
		return mainServerDbAdress;
	}

	public String getMainServerDbName() {
		return mainServerDbName;
	}

	public String getBackupServerAdress() {
		return backupServerAdress;
	}

	public int getBackupServerPort() {
		return backupServerPort;
	}

	public String getBackupServerDbAdress() {
		return backupServerDbAdress;
	}

	public String getBackupServerDbName() {
		return backupServerDbName;
	}

	public String getNorthServerAdress() {
		return northServerAdress;
	}

	public int getNorthServerPort() {
		return northServerPort;
	}

	public String getNorthServerDbAdress() {
		return northServerDbAdress;
	}

	public String getNorthServerDbName() {
		return northServerDbName;
	}

	public String getCenterServerAdress() {
		return centerServerAdress;
	}

	public int getCenterServerPort() {
		return centerServerPort;
	}

	public String getCenterServerDbAdress() {
		return centerServerDbAdress;
	}

	public String getCenterServerDbName() {
		return centerServerDbName;
	}

	public String getSouthServerAdress() {
		return southServerAdress;
	}

	public int getSouthServerPort() {
		return southServerPort;
	}

	public String getSouthServerDbAdress() {
		return southServerDbAdress;
	}

	public String getSouthServerDbName() {
		return southServerDbName;
	}

	public int getCOUPONS_FOR_MAIN_SERVER() {
		return COUPONS_FOR_MAIN_SERVER;
	}
	

	/**
	 *The function converts the category int constant to a string object.
	 *the default (if no valid category key was entered): NO_CATEGORY. 
	 * @param category - category constant.
	 * @return String - the name of the category.
	 */
	public static String categoryToString(int category)
	{
		if (category==FOOD) return _FOODstr;
		if (category==EVENTS) return _EVENTSstr;
		if (category==ELECTRICITY) return _ELECTRICITYstr;
		if (category==VACATIONS) return _VACATIONSstr;
		if (category==FASHION) return _FASHIONstr;
		if (category==ADULTS_ONLY) return _ADULTS_ONLYstr;
		return _NO_CATEGORYstr;
	}
	
	/**
	 * The method return all the available Categories in {@link String}
	 * @return {@link Object[]} of all the Categories in {@link String} format
	 * @author Sapir
	 */
	public static Object[] getCategories()
	{
		Object[] objArray = {"All Categories" ,_ADULTS_ONLYstr, _ELECTRICITYstr, _EVENTSstr, _FASHIONstr, _FOODstr, _VACATIONSstr};
		return objArray;
	}
	
	/**
	 * The method convert a given category in {@link String} to its representation in <code>int</code>  
	 * @param categoryName - {@link String} that is a category
	 * @return int representation of any given <code>categoryName</code><br><br>
	 * <strong>The method may return -1 if <code>categoryName</code> doesn't match any category!!!
	 *  
	 * @author Sapir
	 */
	public static int categoryNamestrToint (String categoryName)
	{
		if (categoryName.equals(_ADULTS_ONLYstr) )
		{
			return ADULTS_ONLY;
		}
		if (categoryName.equals(_ELECTRICITYstr) )
		{
			return ELECTRICITY;
		}
		if (categoryName.equals(_EVENTSstr) )
		{
			return EVENTS;
		}
		if (categoryName.equals(_FASHIONstr) )
		{
			return FASHION;
		}
		if (categoryName.equals(_FOODstr) )
		{
			return FOOD;
		}
		if (categoryName.equals(_VACATIONSstr) )
		{
			return VACATIONS;
		}
		
		return -1;
		
	}
	
	/**
	 *The function converts the region int constant to a string object.
	 *the default (if no valid region key was entered): NO_REGION. 
	 * @param region - category constant.
	 * @return String - the name of the region.
	 */
	public static String regionToString(int region)
	{
		if (region==NORTH) return "North";
		if (region==CENTER) return "Center";
		if (region==SOUTH) return "South";
		if (region==GENERAL) return "General (All Regions)";
		return "NO_REGION";
	}

}
